import org.testng.annotations.Test;

import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @ProjectName: ybxy6
 * @Titile: StremDemo
 * @Author: 86183
 * @Description: TODO
 */
public class StreamDemo {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>();

        names.add("张三");
        names.add("李四");
        names.add("张三1");
        names.add("张三2");
        names.add("张三3");
        names.add("张三4");
        names.add("张三5");
        List<String> list1 = new ArrayList<>();
        //遍历
        names.forEach(name -> {
            if (name.startsWith("张")) {
                list1.add(name);
            }
        });
        List<String> list2 = new ArrayList<>();
        list1.forEach(name -> {
            if (name.length() > 3) {
                list2.add(name);
            }
        });
        list2.forEach(System.out::println);
        System.out.println("--------------------------------");
        //一.创建流
        //1.获取流
        //2.过滤流
        //3.终结方法
        names.stream().filter(s -> s.startsWith("张"))
                .filter(s -> s.length() > 3)
                .forEach(System.out::println);
    }
public void test1(){
List<Integer> list=new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
list.add(7);
list.add(8);
list.add(9);
list.add(10);
List<Integer> List=list.stream().filter(integer->integer % 2==0).toList();
System.out.println(list);
}
        @Test
        public void test3(){
    List<String>list1=new ArrayList<>();
    list1.add("张三");
    list1.add("张三1");
    list1.add("张三2");
    list1.add("张三3");
    List<String>list2=new ArrayList<>();
    list2.add("张四");
    list2.add("张四1");
    list2.add("张四2");
    list2.add("张四3");
    list2.add("张四4");

    Stream<String> stream1=list1.stream().filter(new Predicate<String> (){
        @Override
        public boolean test(String s) {
            return s.length()==3;
        }
    }).limit(2);

    Stream<String> stream2 = list1.stream().filter(new Predicate<String>() {
        @Override
        public boolean test(String s) {
            return s.startsWith("张");
        }
    }).skip(1);
            Stream.concat(stream1,stream2).forEach(System.out::println);
        }
    public void test2() {
        List<String> list = new ArrayList<>();
        list.add("张三,25");
        list.add("张三1,11");
        list.add("张三2,23");
        Map<Object, Object> map = list.stream().filter(s -> {

            String[] split = s.split(",");
            int age = Integer.parseInt(split[1]);
            return age >= 24;
        }).collect(Collectors.toMap((s -> {
            String[] split = s.split(",");
            return split[0];
        }),s -> {
            String[] split = s.split(",");
            return split[1];
        }));
        map.forEach((k, v) -> {
            System.out.println(k + " " + v);
        });
    }
}